<?php

namespace App\Http\Validate\Kms\Album;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Kms\Album\AlbumMainModel;
use App\Models\Kms\Album\AlbumResourceModel;
use Illuminate\Support\Carbon;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AlbumResourceValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [
            "mid" => [
                "required",
                "int",
                Rule::exists((new AlbumMainModel())->getTable(), "id")
                    ->where("ownerUid", attributeGet(request()->user(), "rmsUserId", 0))
            ]
        ];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [
            "mid"                                  => "资源主信息id",
            "albumResourceGroup.*.weather"         => "天气",
            "albumResourceGroup.*.creatorUsername" => "创建人昵称",
            "albumResourceGroup.*.type"            => "资源类型",
            "albumResourceGroup.*.originalUrl"     => "资源原始路径",
            "albumResourceGroup.*.previewUrl"      => "资源预览路径",
            "albumResourceGroup.*.long"            => "经度",
            "albumResourceGroup.*.lat"             => "纬度",
            "albumResourceGroup.*.addressPrefix"   => "地址前缀 - 省市区",
            "albumResourceGroup.*.addressSuffix"   => "地址后缀 - 详细地址",
            "albumResourceGroup.*.videoDuration"   => "视频时长",
            "albumResourceGroup.*.size"            => "required|int|gt:0",
            "albumResourceGroup.*.shootingTime"    => "拍摄时间",
            "albumResourceGroup.*.poiSource"       => "poi来源",
            "albumResourceGroup.*.poiType"         => "poi类型",
            "albumResourceGroup.*.poiNo"           => "poi编号",
            "albumResourceGroup.*.poiName"         => "poi名称",
            "albumResourceGroup.*.resolutionRatio" => "资源分辨率"
        ];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function uploadValidate(array $params)
    {
        return self::getInstance()->validate(
            ["albumResourceGroup", "mid"],
            $params,
            [
                "mid"                                  => [
                    "required",
                    "int",
                    Rule::exists((new AlbumMainModel())->getTable(), "id")
                        ->where("ownerUid", request()->user()->rmsUserId)
                ],
                "albumResourceGroup"                   => [
                    "required",
                    "array",
                    "min:1"
                ],
                "albumResourceGroup.*.long"            => self::stringValidateBuild(false, 32, 1),
                "albumResourceGroup.*.lat"             => self::stringValidateBuild(false, 32, 1),
                "albumResourceGroup.*.addressPrefix"   => self::stringValidateBuild(false, 48, 1),
                "albumResourceGroup.*.addressSuffix"   => "string|max:120",
                "albumResourceGroup.*.weather"         => self::stringValidateBuild(false, 36, 1),
                "albumResourceGroup.*.creatorUsername" => self::stringValidateBuild(true, 36, 1),
                "albumResourceGroup.*.type"            => self::intInValidateBuild(false,
                    AlbumResourceModel::TYPE_GROUP),
                "albumResourceGroup.*.originalUrl"     => "required|string|url",
                "albumResourceGroup.*.previewUrl"      => "required|string|url",
                "albumResourceGroup.*.size"            => "required|int|gt:0",
                "albumResourceGroup.*.videoDuration"   => "required|int|gte:0",
                "albumResourceGroup.*.shootingTime"    => self::timeValidateBuild(true),
                "albumResourceGroup.*.poiSource"       => self::intInValidateBuild(
                    false,
                    AlbumResourceModel::POI_SOURCE_TEXT
                ),
                "albumResourceGroup.*.poiNo"           => self::stringValidateBuild(false, 32, 1),
                "albumResourceGroup.*.poiType"         => self::strInValidateBuild(
                    false,
                    AlbumResourceModel::POI_TYPE_TEXT,
                    ["ALL"]
                ),
                "albumResourceGroup.*.poiName"         => self::stringValidateBuild(false, 120, 1),
                "albumResourceGroup.*.resolutionRatio" => self::stringValidateBuild(true, 36, 1)

            ],
            true,
            function ($params) {
                $params["albumResourceGroup"] = collect($params["albumResourceGroup"])->map(function ($v) {
//                    $v["originalUrl"]  = getPrivateFileKey($v["originalUrl"]);
//                    $v["previewUrl"]   = getPrivateFileKey($v["previewUrl"]);
                    $v["shootingTime"] = strtotime($v["shootingTime"]);
                    $v["weekDay"]      = Carbon::createFromTimestamp(strtotime($v["shootingTime"]))->weekday() + 1;

                    return $v;
                })->values()->toArray();
                return $params;
            }
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function joinRecycleBinValidate(array $params)
    {
        return self::getInstance()->validate(
            ["mid", "ridGroup"],
            $params,
            [
                "ridGroup"   => [
                    "required",
                    "array",
                    "min:1"
                ],
                "ridGroup.*" => [
                    "int",
                    Rule::exists((new AlbumResourceModel())->getTable(), "id")
                        ->where("mid", attributeGet($params, "mid", 0))
                        ->where("isInRecycleBin", AlbumResourceModel::IS_IN_RECYCLE_BIN_NO)
                        ->where("isDeleted", 0)
                ]
            ]
        );
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function resetRecycleBinValidate(array $params)
    {
        return self::getInstance()->validate(
            ["mid", "ridGroup"],
            $params,
            [
                "ridGroup"   => [
                    "required",
                    "array",
                    "min:1"
                ],
                "ridGroup.*" => [
                    "int",
                    Rule::exists((new AlbumResourceModel())->getTable(), "id")
                        ->where("mid", attributeGet($params, "mid", 0))
                        ->where("isInRecycleBin", AlbumResourceModel::IS_IN_RECYCLE_BIN_YES)
                        ->where("isDeleted", 0)
                ]
            ]
        );
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function batchResetRecycleBinValidate(array $params)
    {
        return self::getInstance()->validate(["mid"], $params);
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function delValidate(array $params)
    {
        return self::getInstance()->validate(
            ["mid", "ridGroup"],
            $params,
            [
                "ridGroup"   => [
                    "required",
                    "array",
                    "min:1"
                ],
                "ridGroup.*" => [
                    "int",
                    Rule::exists((new AlbumResourceModel())->getTable(), "id")
                        ->where("mid", attributeGet($params, "mid", 0))
                        ->where("isInRecycleBin", AlbumResourceModel::IS_IN_RECYCLE_BIN_YES)
                        ->where("isDeleted", 0)
                ]
            ]
        );
    }

    /**
     * 回收站删除校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function batchDelValidate(array $params)
    {
        return self::getInstance()->validate(["mid"], $params);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function listValidate(array $params)
    {
        return self::getInstance()->validate(
            ["mid", "lastTime", "lastId"],
            $params,
            [
                "lastTime" => [
                    "required",
                    "int",
                    "gte:0"
                ],
                "lastId"   => [
                    "required",
                    "int",
                    "gte:0"
                ]
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function recycleBinListValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "mid",
                "pageNum",
                "pageSize"
            ],
            $params, [
            "pageNum"  => "int|min:1",
            "pageSize" => "int|min:1"
        ]);
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function resourceListValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "pageNum",
                "pageSize",
                "status"
            ],
            $params, [
            "pageNum"  => "int|min:1",
            "pageSize" => "int|min:1",
            "status"   => self::intInValidateBuild(true, AlbumResourceModel::CRUISE_CHECK_STATUS_TEXT)
        ]);
    }
}
